﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;

namespace PhotoFrame
{
  class FramePictureManager
  {
    class FrameResourceInfo
    {
      public string ImageFile;
      public string TxtFile;
    }
    class FrameInformation
    {
      public Image Frame;
      public Rectangle Region;

      public static FrameInformation fromFile(string image_name, string txt_name)
      {
        try
        {
          FrameInformation info = new FrameInformation();
          info.Frame = Image.FromFile(image_name);

          StreamReader txt = new StreamReader(txt_name, Encoding.Default);
          string line = txt.ReadLine();
          if (line != null)
          {
            string[] datas = line.Split(',');
            if (datas.Length != 4)
              return null;

            info.Region.X = Convert.ToInt32(datas[0]);
            info.Region.Y = Convert.ToInt32(datas[1]);
            info.Region.Width = Convert.ToInt32(datas[2]);
            info.Region.Height = Convert.ToInt32(datas[3]);
          }
          return info;
        }
        catch (System.Exception ex)
        {
          Console.WriteLine(ex.ToString());
          return null;
        }
      }
    }

    /// <summary>
    ///   默认相框目录
    /// </summary>
    private static string DefaultFramePicturePath = "Frames";
    /// <summary>
    ///   相框图片控件
    /// </summary>
    private zcode.FramePictureBox _pictureBox;
    /// <summary>
    ///   相框列表
    /// </summary>
    private List<FrameInformation> _frames = new List<FrameInformation>();
    /// <summary>
    ///   当前显示相框
    /// </summary>
    private int _current_show = 0;

    public FramePictureManager(zcode.FramePictureBox pictureBox)
    {
      _pictureBox = pictureBox;
    }

    public void load()
    {
      try
      {
        string[] strs = Directory.GetFiles(DefaultFramePicturePath, "*.*");
        if (strs.Length <= 0)
          return;

        Dictionary<string, FrameResourceInfo> file_infos = new Dictionary<string, FrameResourceInfo>();
        foreach (string file in strs)
        {
          string file_name = Path.GetFileNameWithoutExtension(file);
          if (!file_infos.ContainsKey(file_name))
            file_infos.Add(file_name, new FrameResourceInfo());

          FileInfo fi = new FileInfo(file);
          if (fi.Extension == ".jpg"
            || fi.Extension == ".png"
            || fi.Extension == ".bmp")
          {
            file_infos[file_name].ImageFile = file;
          }
          else if (fi.Extension == ".txt")
          {
            file_infos[file_name].TxtFile = file;
          }
        }

        foreach (var file_info in file_infos)
        {
          FrameResourceInfo re_info = file_info.Value;
          if (re_info.ImageFile != null &&
            re_info.TxtFile != null)
          {
            FrameInformation info = FrameInformation.fromFile(re_info.ImageFile, re_info.TxtFile);
            if (info != null)
              _frames.Add(info);
          }
        }
      }
      catch (System.Exception ex)
      {
        Console.WriteLine(ex);
      }
    }

    public void show()
    {
      if (_current_show >= _frames.Count)
        return;

      FrameInformation info = _frames[_current_show];
      _pictureBox.PictureRegion = info.Region;
      _pictureBox.FrameImage = info.Frame;
    }

    public void preFrame()
    {
      if (_frames.Count < 1)
        return;

      if (_current_show == 0)
        _current_show = _frames.Count - 1;
      else
        --_current_show;
    }

    public void nextFrame()
    {
      if (_frames.Count < 1)
        return;

      if (_current_show >= _frames.Count - 1)
        _current_show = 0;
      else
        ++_current_show;
    }

    public void addFrame(Image frame, Rectangle region)
    {
      FrameInformation info = new FrameInformation();
      info.Frame = frame;
      info.Region = region;
      _frames.Add(info);
    }    
  }
}
